package com.wf.smartcomm.server.client.action;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.aspectj.util.FileUtil;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.multipart.MultipartFile;

import com.wf.smartcomm.server.util.IdUtils;
import com.wf.smartcomm.server.util.ImageUtils;
import com.wf.smartcomm.server.util.PushUtils;

@Controller
@Scope("prototype")
public class CkeditorUpload extends BaseAction {
	
	private List<File> upload;  
    // /多个上传文件的类型集合  
    private List<String> uploadContentType;  
   // 多个上传文件的文件名集合  
    private List<String> uploadFileName;
    
	public List<File> getUpload() {
		return upload;
	}

	public void setUpload(List<File> upload) {
		this.upload = upload;
	}

	public List<String> getUploadContentType() {
		return uploadContentType;
	}

	public void setUploadContentType(List<String> uploadContentType) {
		this.uploadContentType = uploadContentType;
	}

	public List<String> getUploadFileName() {
		return uploadFileName;
	}

	public void setUploadFileName(List<String> uploadFileName) {
		this.uploadFileName = uploadFileName;
	}
	
	public void ckeditorUpload()throws Exception{
		if (upload != null && upload.size() > 0) {
			response.setCharacterEncoding("UTF-8");
			PrintWriter out = response.getWriter();  
		    String callback =request.getParameter("CKEditorFuncNum");
		    String contentType = uploadContentType.get(0);
		    if (contentType.equals("image/jpeg") || contentType.equals("image/pjpeg") || contentType.equals("image/png") || contentType.equals("image/x-png") ||contentType.equals("image/gif") || contentType.equals("image/bmp")){
		    	
				String fileName = IdUtils.uuid()+".png";
				String url = "http://"+PushUtils.pushConfig.getProperty("ImageServerIp");
				if (request.getServerPort() != 80) {
					url+=":"+request.getServerPort();
				}
				url+=request.getContextPath()+"/thumbImages/";
				String path = request.getServletContext().getRealPath(
						"images");
				File des = new File(path + File.separator + fileName);
				FileUtil.copyFile(upload.get(0), des);
				BufferedImage bufferedImage = ImageIO.read(des);
				int width = bufferedImage.getWidth();   
				int height = bufferedImage.getHeight();
				ImageUtils.getThumbnail(des.getAbsolutePath(),
						request.getServletContext().getRealPath(
								"thumbImages")+File.separator+fileName, 150, 150*height/width);
				out.println("<script type=\"text/javascript\">");    
		        out.println("window.parent.CKEDITOR.tools.callFunction(" + callback + ",'" + url+fileName+ "','')");    
		        out.println("</script>"); 
		    }else {
		    	out.println("<script type=\"text/javascript\">");    
	            out.println("window.parent.CKEDITOR.tools.callFunction(" + callback + ",''," + "'文件格式不正确（必须为.jpg/.gif/.bmp/.png文件）');");   
	            out.println("</script>");
			}
		}
	}
}
